<?php

    require_once(dirname(__FILE__) . '/../includes/common.inc.php');

    $articleId = trim(Request::GetParam('id'));
    $article = $articleId ? ArticleTable::GetItemById($articleId) : new ArticleInfo();

    if ( Request::IsMethod(Request::POST) )
    {
        ProcessForm($article);
    }

    $applyToOptions = array
    (
        Product::CODEDRIVE => Product::GetName( Product::CODEDRIVE )
    );

    $applyTo = array();
    if ( !$article->GetArticleId() )
    {
        $pageTitle = 'New Article';
    }
    else
    {
        $pageTitle = 'Edit Article';

        $applyTo = ArticleTable::GetProducts($article->GetArticleId());
    }

    $publishedDate = $article->GetPublishDate();
    $publishedDate = ((empty($publishedDate)) ? time() : $publishedDate);

    $page = Pages::CreatePage( $pageInfo->id );
    $page->SetVar('article_id', $article->GetArticleId());
    $page->SetVar('category_id', $article->GetCategoryId());
    
    $categories = GetCategoryOptions();    

    $page->SetVar('category_options', Util::GetSelectOptions(array_values($categories), array_keys($categories), false ) );
    
    $page->SetVar('page_title', $pageTitle);
    $page->SetVar('title', $article->GetTitle());
    $page->SetVar('slug', $article->GetSlug());
    $page->SetVar('brief_description', $article->GetExcerpt());
    $page->SetVar('meta_title', $article->GetMetaTitle());
    $page->SetVar('content', $article->GetContent());
    $page->SetVar('is_published', $article->GetIsPublished());
    $page->SetVar('publish_date', date(DateUtils::ISO_DATE_PATTERN, $publishedDate));
    $page->SetVar('tags', $article->GetTags());
    $page->SetVar('return_url', Core::App()->GetUser()->GetReturnUrl( Pages::GetPageUrl('AdminArticles') ));
    $page->SetArrVar('language_options', Util::GetSelectOptions( CodeDriveSiteLanguage::GetSupportedLanguages(), $article->GetLanguage(), false ) ) ;
    $page->SetArrVar('type_options', Util::GetSelectOptions( ArticleType::GetTypes(), $article->GetType(), false ) ) ;
    $page->SetArrVar('product_options', $applyToOptions);
    $page->SetArrVar('products_selected', $applyTo);

    $user = Core::App()->GetUser();
    $page->SetArrVar('return_params', http_build_query( array(
        'filter' => $user->GetState('articles_filter'),
        'page'   => $user->GetState('articles_page')
    ) ));

    $page->Display();


    function GetCategoryOptions()
    {
        $result = array();
        $categories = ArticleCategoryTable::GetCategories();
        foreach ($categories as $category)
        {
            $result[$category->GetCategoryId()] = $category->GetName();
        }

        return $result;
    }

    function ProcessForm(ArticleInfo $article)
    {
        if ( Request::GetParam('action') == 'Delete' )
        {
            if ( $article->GetArticleId() )
            {
                ArticleTable::Delete($article->GetArticleId());

                Request::Redirect( Pages::GetPageUrl('AdminArticles') );
            }
        }

        $type = Request::GetParam('type');
        $article->SetType($type);
        $article->SetCategoryId(trim(Request::GetParam('category_id')));
        $article->SetLanguage(trim(Request::GetParam('language')));
        $article->SetTitle(trim(Request::GetParam('title')));
        $article->SetMetaTitle(trim(Request::GetParam('meta_title', '')));
        $article->SetContent(trim(Request::GetParam('content')));
        $article->SetIsPublished(trim(Request::GetParam('published')) == 'on');
        $article->SetTags(trim(Request::GetParam('tags')));

        if ( $article->GetIsPublished() )
        {
            $publishDate = trim(Request::GetParam('publish_date'));
            if ( !$publishDate )
            {
                $publishDate = time();
            }
            $article->SetPublishDate($publishDate);
        }

        $article->SetExcerpt(trim(Request::GetParam('excerpt', '')));
        
        // Generate SLUG for new articles automatically
        $slug = $article->GetArticleId() ? trim(Request::GetParam('slug')) : '';
        $article->SetSlug($slug);

        $applyTo = Request::GetParam('product');
        $articleId = ArticleTable::SaveArticle($article, $applyTo);

        Request::Redirect( Pages::GetPageUrl('AdminArticle', array('id' => $articleId)) );
    }
?>